﻿<%@ Page Language="C#" %>
<%@ Register TagPrefix="IRL" TagName="SideBar" Src="SideBar.ascx" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>


<script runat="server">
    
    public const int CURRENT_MODULE = 53;
    
    Hashtable ptype_lookup, rcat_lookup;

    protected void Page_Load(object sender, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        
        InitLookUp();
        if (!Page.IsPostBack)
        {
            GenerateTimeline();
        }
    }

    void GenerateTimeline()
    {
        string StrPath = ConfigurationSettings.AppSettings["CMS_Path"].ToString();
        StrPath += @"\timeline.xml";

        if (File.Exists(StrPath))
        {
            File.Delete(StrPath);            
        }
        using (StreamWriter sw = File.CreateText(StrPath))
        {
            sw.WriteLine("<data>");
            // Write Project info
            WriteProjects(sw);
            // Write Milestone info
            WriteMilestone(sw);
            sw.WriteLine("</data>");
            sw.Close();
        }
    }
    
    void InitLookUp()
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            ptype_lookup = Lookup.Fill("sp_Select_cms_Lookup 'project_type'", conn);
            rcat_lookup = Lookup.Fill("sp_Select_cms_Lookup 'research_category_s'", conn);
        }
    }
    
    void WriteProjects(StreamWriter writer)
    {
        string StrProjectName, StrType, StrSector, StrCode;
        string StrContent;
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            string StrImage = "../images/irl_small.gif";
            SqlCommand cmd = new SqlCommand("SELECT p.*, [Company_Name] AS [ClientName] FROM vw_Projects p INNER JOIN Tbl_Company_info c ON p.[ClientID]=c.[Company_ID]", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                StrProjectName = Server.HtmlEncode(dr["ProjectName"].ToString());
                StrType = ptype_lookup[dr["ProjectType"].ToString()].ToString();
                StrSector = rcat_lookup[dr["ResearchCategory"].ToString()].ToString();
                StrCode = dr["ProjectCode"].ToString().Trim();
                StrContent = "Project Code: " + StrCode +
                                "&lt;br />Type : " + StrType +
                                "&lt;br />Sector: " + StrSector +
                                "&lt;br />Value: " + String.Format("{0:c}", dr["Costs"]) +
                                "&lt;br />Client: " + Server.HtmlEncode(dr["ClientName"].ToString()) + "&lt;br />";
                writer.WriteLine("\t<event " +
                                "isDuration=\"true\" " +
                                "start=\"" + dr["StartDate"].ToString() + "\" " +
                                "end=\"" + dr["EndDate"].ToString() + "\" " +
                                "title=\"" + StrProjectName + "\" " +
                                "link=\"saveProject.aspx?pid=" + dr["ProposalID"].ToString() + "\" " +
                                "image=\"" + StrImage + "\">" + StrContent + "</event>");
            }
        }
    }
    void WriteMilestone(StreamWriter writer)
    {
        string StrContent;
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            string StrPaymentDate;
            //SqlCommand cmd = new SqlCommand("SELECT [MilestoneType], [Amount], [PaymentDate], [Value] as [Status] FROM cms_Milestones m INNER JOIN cms_Lookup l ON m.[Status]=l.[ID] WHERE l.[Name]='milestone_status'", conn);
            SqlCommand cmd = new SqlCommand("SELECT [ProjectName], [MilestoneType], [Amount], [PaymentDate], [Value] as [Status] FROM cms_Milestones m INNER JOIN cms_Proposals p ON m.ProposalID=p.ProposalID INNER JOIN cms_Lookup l ON m.[Status]=l.[ID] WHERE l.[Name]='milestone_status'", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                StrPaymentDate = dr["PaymentDate"].ToString();
                StrContent = "Project Name: &lt;b>" + dr["ProjectName"].ToString() + "&lt;/b>" +
                                "&lt;br />Amount: " + String.Format("{0:c}", dr["Amount"]) +
                                "&lt;br />Payment Date : " + DateTime.Parse(StrPaymentDate).ToString("dd-MM-yyyy") +
                                "&lt;br />Status: " + dr["Status"].ToString() + "&lt;br />";
                writer.WriteLine("\t<event " +                                
                                "start=\"" + StrPaymentDate + "\" " +                                
                                "title=\"" + dr["MilestoneType"].ToString() + "\" " +                                
                                ">" + StrContent + "</event>");
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="pragma" content="no-cache" />
    <title>Indochina Research</title>
    <link rel="SHORTCUT ICON" href="../irl.ico" type="image/x-icon" />
    <link type="text/css" rel="stylesheet" href="../css/global.css" />
    <link type="text/css" rel="stylesheet" href="css/cms.css" />
	
    <script src="timeline/api/timeline-api.js" type="text/javascript"></script>
    <script src="timeline/config.js" type="text/javascript"></script>
</head>

<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">

								&nbsp;</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: 
								Project Timeline
						  </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="DataForm" method="post" runat="server">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="left" valign="top" style="padding:0 20px;">
					
						<div id="my-timeline" style="height:370px; border:1px solid #aaa; font-family:Tahoma; font-size:8pt;"></div>
						
					</td>
				</tr>
			</table>
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
